package final_wearebatman_view;

import final_wearebatman_model.Scope;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

public class ScopeView implements Ipaintable {

    private Scope scope;
    private CircuitView circuitview;
    private int posX;
    private int posY;
    private int angle;
    private String name;

    // konstruktor
    public ScopeView(Scope s, String name, CircuitView cv, int pX, int pY, int a) {
        this.scope = s;
        this.circuitview = cv;
        this.posX = pX;
        this.posY = pY;
        this.angle = a;
        this.name = name;
    }

    // kirajzolas
    public void paint(Graphics2D g) {
        AffineTransform aT = g.getTransform();
        g.translate(posX, posY);
        g.rotate(this.angle * Math.PI / 180);
        g.drawRect(0, 0, 202, 30);
        g.setColor(new Color(250,250,250));
        for(int i=1; i<20; i++)
            g.drawLine(1 + i * 10, 1, 1 + i * 10, 29);
        g.setColor(Color.red);
        String s = this.scope.getFinalState();
        String ss = s.substring(s.length() - 20 < 0 ? 0 : s.length() - 20, s.length());
        if (ss.length() > 0) {
            for (int i = 0; i < ss.length(); i++) {
                if (ss.charAt(i) == '0') {
                    g.drawLine(1 + i * 10, 28, 11 + i * 10, 28);
                } else {
                    g.drawLine(1 + i * 10, 2, 11 + i * 10, 2);
                }
                if (i > 0) {
                    if (ss.charAt(i - 1) != ss.charAt(i)) {
                        g.drawLine(1 + i * 10, 2, 1 + i * 10, 28);
                    }
                }
            }
        }
        g.setColor(Color.black);
        g.setTransform(aT);
    }

    public int getPosX() {
        return this.posX;
    }

    public int getPosY() {
        return this.posY;
    }

    public int getAngle() {
        return this.angle;
    }

    public Point getInputPinPosition(int id) {
        Point p = new Point(0, 15);
        AffineTransform aT = new AffineTransform();
        aT.rotate(this.angle * Math.PI / 180);
        aT.transform(p, p);
        return new Point(posX + p.x, posY + p.y);
    }

    public Point getOutputPinPosition(int id) {
        return new Point(posX, posY);
    }

    public Rectangle getClickArea() {
        return new Rectangle();
    }

    public void onClick() {

    }
}
